package www.golf.web.util;

import org.zkoss.zk.au.out.AuScript;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.HtmlBasedComponent;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Slider;


public class UtilTools {
	UtilEvent utilEvent=new UtilEvent();
	/**
	 * 執行js
	 * @author angle
	 * @param obj 丟入string 或是 StringBuffer
	 */
	public void exeZulJavascript(Object obj){
		
		if(obj instanceof StringBuffer && obj!=null && !"".equals(obj.toString())){
			//System.out.println("=============\n" + obj.toString());
			Executions.getCurrent().addAuResponse(null,
					new AuScript(null, obj.toString()));
		}else if(obj instanceof String && obj!=null && !"".equals(obj.toString())){
			//System.out.println("=============\n" + obj.toString());
			Executions.getCurrent().addAuResponse(null,
					new AuScript(null, obj.toString()));
		}else if(!(obj instanceof String)&&!(obj instanceof StringBuffer)){
			System.out.println("exeZulJavascript is not suport");
		}
	}
	
	
	/**
	 * 框架調整工具
	 * @author angle
	 * @param slider 建構Slider物件
	 * @param component 需要調整的元件
	 * @param type 字串型態"width"或是"high"
	 * @param lsize 最大size
	 * @param ssize 最小size
	 * @param show 是否顯示
	 */
	public void structHWSlider(final Slider slider,final Component component,final String type,final int lsize,final int ssize,boolean show){
		try{
			slider.setMold("sphere");
			slider.addEventListener("onScroll", new EventListener() {
				public void onEvent(org.zkoss.zk.ui.event.Event event) throws Exception {
					if("width".equals(type)){
						((HtmlBasedComponent) component).setWidth(String.valueOf((slider.getCurpos()*((lsize-ssize)/100))+ssize)+"px");
					}else if("high".equals(type)){
						((HtmlBasedComponent) component).setHeight(String.valueOf((slider.getCurpos()*((lsize-ssize)/100))+ssize)+"px");
					}
				}
			});		
			slider.setVisible(show);
		}catch(Exception e){
			System.out.println("structSlider error!!");
			e.printStackTrace();
		}
	}
	
}
